<?php
/**
 * 站点管理，添加站点实现单点登录
 * @author M2
 * @version 1.0
 * create Date 2013-05-21
 */
class SiteAction extends CommonAction {
	/**
	 * 添加站点
	 * M2
	 */
	public function addSite() {
		if($_POST) {
			$data['name'] = common('String')->getgpc('appName', 'P');
			$data['url'] = common('String')->getgpc('appsUrl', 'P');
			$data['ip']  = common('String')->getgpc('appsIp', 'P');
			$commKey = common('String')->getgpc('communicationKey', 'P');
			$data['authkey'] = common('String')->authcode($commKey, 'ENCODE', 'cs5H7Z2bbC9lfi6ift7DaDfQfH6r0U5M4C755l5K6908dD8Nahdz9U6YbX2E2J7Q');
			if(!common('Url')->check_url($_POST['appsUrl'])) {
				$this->error(L('addressEntryError'));
			}
			if(!empty($_POST['appsIp']) && !common('Url')->check_ip($_POST['appsIp'])) {
				$this->error(L('ipAddressIsNot'));
			}
			$checkAppName = model('Applications')->checkAppName(common('String')->getgpc('appName', 'P'));
			if($checkAppName) {
				$this->error(L('appNameAlreadyExists'));
			} else {
				if(M('Applications')->add($data)) {
					$this->assign('jumpUrl',URL('admin/Site/siteManager'));
					$this->success(L('addSuccess'));
				}
			}
		} else {
			$this->display();
		}
	}

	/**
	 * 站点管理
	 * M2
	 */
	public function siteManager() {
		$status = $affectedrows = 0;
		$applist = model('Applications')->get_apps();
		$this->assign('applist', $applist);
		$this->display();
	}

	public function ping() {
		$ip = common('String')->getgpc('ip');
		//$url = common('String')->getgpc('url');
		$appid = intval(common('String')->getgpc('appid'));
		$url = common('Url')->get_url_code('test','', $appid);
		$status = common('Url')->test_api($url, $ip);
		if($status == '1') {
			echo 'document.getElementById(\'status_'.$appid.'\').innerHTML = "<img src=\'./Public/admin/correct.gif\' border=\'0\' class=\'statimg\' \/><span class=\'green\'>'.L('app_connent_ok').'</span>";testlink();';
		} else {
			echo 'document.getElementById(\'status_'.$appid.'\').innerHTML = "<img src=\'./Public/admin/error.gif\' border=\'0\' class=\'statimg\' \/><span class=\'red\'>'.L('app_connent_false').'</span>";testlink();';
		}
	}

	/**
	 * 删除应用信息
	 */
	public function doDeleteApps() {
		$ids = $_POST['ids'];
    	if ( empty($ids) ) {echo 0; return ;}

    	$_LOG['uid'] = $this->mid;
    	$_LOG['type'] = '2';
    	$data[] = L("manager")."-".L("siteManager")."-".L("siteList");
    	$_LOG['data'] = serialize($data);
    	$_LOG['ctime'] = time();
    	M('AdminLog')->add($_LOG);

    	$res = M('Applications')->where("appid in(".$ids.")")->delete();
    	if ($res){
    		echo 1;
    	}else{
    		echo 0;
    	}
	}

	/**
	 * 编辑应用信息
	 * M2
	 */
	public function editApps() {
		$id = $_GET['id'];
		if(empty($id)) return false;
		$appinfo = M('Applications')->where("appid=$id")->find();
		$appinfo['authkey'] = common('String')->authcode($appinfo['authkey'], 'DECODE', 'cs5H7Z2bbC9lfi6ift7DaDfQfH6r0U5M4C755l5K6908dD8Nahdz9U6YbX2E2J7Q');
		$this->assign('appinfo',$appinfo);
		$this->display('addSite');
	}

	/**
	 * 更新应用信息
	 * M2
	 */
	public function editSite() {
		$appid = common('String')->getgpc('siteappid', 'P');
		$data['name'] = common('String')->getgpc('appName', 'P');
		$data['url'] = common('String')->getgpc('appsUrl', 'P');
		$data['ip']  = common('String')->getgpc('appsIp', 'P');
		$commKey = common('String')->getgpc('communicationKey', 'P');
		$data['authkey'] = common('String')->authcode($commKey, 'ENCODE', 'cs5H7Z2bbC9lfi6ift7DaDfQfH6r0U5M4C755l5K6908dD8Nahdz9U6YbX2E2J7Q');
		if(!common('Url')->check_url($_POST['appsUrl'])) {
			$this->error(L('addressEntryError'));
		}
		if(!empty($_POST['appsIp']) && !common('Url')->check_ip($_POST['appsIp'])) {
			$this->error(L('ipAddressIsNot'));
		}
		$checkAppName = model('Applications')->checkAppName(common('String')->getgpc('appName', 'P'),$appid);
		if(!empty($checkAppName)) {
			$this->error(L('appNameAlreadyExists'));
		} else {
			if(M('Applications')->where("appid=$appid")->save($data)) {
				if($appid == '1') {
					$ylconfigfile = SITE_PATH.'/config_ocean.php';
					$arr = array_change_key_case(include $ylconfigfile);
					$arr['yl_key'] = $commKey;
					$arr['yl_ip'] = $data['ip'];
					file_put_contents($ylconfigfile, "<?php\nreturn " . var_export($arr, true) . ";\n?>");
				}
				$this->assign('jumpUrl',URL('admin/Site/siteManager'));
				$this->success(L('editSuccess'));
			}
		}
	}
}
?>
